#!/usr/bin/python
# -*- coding: UTF-8 -*-

from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility
from extension.test_logic.FileTransport import sftp_stream_copy
from datetime import datetime
import time


class IPMCSET_FAN_001(Case):
    """
            Name:
                IPMCSET_FAN_001
            Label:
                设置风扇运行速度
            PreCondition:
                BMC正常启动
            TestStep:
                1) 命令行执行 ipmcset -d fanmode -v 1 0，ipmcset -d fanlevel -v 50 2 设置生效后查询风扇信息 ipmcget -d faninfo
            ExpectedResult:
                A) 查询信息和设置一致
            Parameters:

            Author:
                baixin liuya
            Last Modified:

            Remark:

        """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.logger.info("Pre Test Case Start.... ")
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.bmc_host = self.device_dut.get_host(host_type=HostType.BMC)

    # 测试步骤
    def procedure(self):
        self.log_step("Step1: 查询风扇信息")
        resp = self.bmc_host.run({"command": ["ipmcget -d faninfo"], "timeout": 10})
        self.logger.info(resp)

        self.log_step("Step2: 设置风扇[ipmcset -d fanmode -v 1 0]")
        self.bmc_host.run({"command": ["ipmcset -d fanmode -v 1 0"], "timeout": 10})
        resp = self.bmc_host.run({"command": ["ipmcget -d faninfo"], "timeout": 10})
        self.logger.info(resp)

        self.log_step("Step3: 设置风扇2[ipmcset -d fanlevel -v 50 2]")
        self.bmc_host.run({"command": ["ipmcset -d fanlevel -v 50 2"], "timeout": 10})
        resp = self.bmc_host.run({"command": ["ipmcget -d faninfo"], "timeout": 10})
        self.logger.info(resp)
        if resp['rc'] != 0:
            self.assertIsNotNone(None, f"设置风扇2失败：{resp['stderr']}")

        if "Fan2: 50" not in resp['stdout']:
            self.assertIsNotNone(None, f"设置风扇2失败：{resp['stdout']}")

        self.log_step("Step3: 设置风扇mode 为 auto[ipmcset -d fanmode -v 0]")
        resp = self.bmc_host.run({"command": ["ipmcset -d fanmode -v 0"], "timeout": 10})
        resp = self.bmc_host.run({"command": ["ipmcget -d faninfo"], "timeout": 10})
        self.logger.info(resp)

    # 测试结束后恢复环境
    def post_test_case(self):
        pass